- Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy path1383. Maximum Performance of a Team.go
48 lines (43 loc) · 1.04 KB
/
1383. Maximum Performance of a Team.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package leetcode
import (
"container/heap"
"sort"
)
funcmaxPerformance(nint, speed []int, efficiency []int, kint) int {
indexes:=make([]int, n)
fori:=rangeindexes {
indexes[i] =i
}
sort.Slice(indexes, func(i, jint) bool {
returnefficiency[indexes[i]] >efficiency[indexes[j]]
})
ph:=speedHeap{}
heap.Init(&ph)
speedSum:=0
varmaxint64
for_, index:=rangeindexes {
ifph.Len() ==k {
speedSum-=heap.Pop(&ph).(int)
}
speedSum+=speed[index]
heap.Push(&ph, speed[index])
max=Max(max, int64(speedSum)*int64(efficiency[index]))
}
returnint(max% (1e9+7))
}
typespeedHeap []int
func (hspeedHeap) Less(i, jint) bool { returnh[i] <h[j] }
func (hspeedHeap) Swap(i, jint) { h[i], h[j] =h[j], h[i] }
func (hspeedHeap) Len() int { returnlen(h) }
func (h*speedHeap) Push(xinterface{}) { *h=append(*h, x.(int)) }
func (h*speedHeap) Pop() interface{} {
res:= (*h)[len(*h)-1]
*h= (*h)[:h.Len()-1]
returnres
}
funcMax(a, bint64) int64 {
ifa>b {
returna
}
returnb
}